package com.mjlf.mjatis.core;

import com.mjlf.mjatis.config.Configuration;
import com.mjlf.mjatis.mapper.MapperProxyFactory;

/**
 * @ClassName AbstractSqlSession
 * @Author mjlft
 * @Date 2020/5/26 23:49
 * @Version 1.0
 * @Description TODO
 */
public abstract class AbstractSqlSession implements SqlSession{

    protected Configuration configuration;
    protected MapperProxyFactory mapperProxyFactory;

    @Override
    public Configuration getConfiguration() {
        return this.configuration;
    }

    @Override
    public void setConfiguration(Configuration configuration) {
        this.configuration = configuration;
    }

    @Override
    public MapperProxyFactory getMapperProxyFactory() {
        if(this.mapperProxyFactory == null){
            synchronized (AbstractSqlSession.class){
                if(this.mapperProxyFactory == null){
                    this.mapperProxyFactory = new MapperProxyFactory(this.configuration);
                }
            }
        }
        return mapperProxyFactory;
    }

    /**
     * 需要注意这里由并发安全
     * @param configuration
     */
    public void flushConfiguration(Configuration configuration) {
        flushDataSource();
        flushEnvironment();
        flushAlisa();
        flushMapper();
        flushPlugins();
    }
}
